package com.su.lightthings.fragment.device

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.su.lightthings.R
import com.su.lightthings.adapter.WatchDetailDataAdapter
import com.su.lightthings.adapter.WatchDetailSettingAdapter
import com.su.lightthings.base.BaseFragment
import com.su.lightthings.databinding.FragmentWatchDetailBinding
import com.su.lightthings.utils.Cons
import com.su.lightthings.view.LinearSpacingItemDecoration

class WatchDetailFragment : BaseFragment<FragmentWatchDetailBinding>() {

    private val mDataAdapter: WatchDetailDataAdapter by lazy {
        WatchDetailDataAdapter()
    }
    private val mSettingAdapter: WatchDetailSettingAdapter by lazy {
        WatchDetailSettingAdapter()
    }

    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentWatchDetailBinding =
        FragmentWatchDetailBinding.inflate(layoutInflater)

    override fun initViews() {
        binding.rvData.apply {
            adapter = mDataAdapter
            layoutManager = GridLayoutManager(context, 2)
            isNestedScrollingEnabled = false
            addItemDecoration(LinearSpacingItemDecoration(top = 10, bottom = 10, left = 10, right = 10))
        }

        binding.rvSetting.apply {
            adapter = mSettingAdapter
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            isNestedScrollingEnabled = false
            addItemDecoration(LinearSpacingItemDecoration(top = 10, bottom = 10))
        }

        mDataAdapter.submitList(Cons.FunctionItemList.watchDetailDataList)
        mSettingAdapter.submitList(Cons.FunctionItemList.mainFunctionList)

        mSettingAdapter.setOnItemClickListener { _, _, i ->
            Log.d("TAG", "initViews: $i")
            findNavController().navigate(R.id.action_watchDetailFragment_to_watchFunctionFragment,
                Bundle().apply {
                    putInt("index", i)
                }
            )
        }
    }
}